/*
 * N900Parse.h
 *
 *  Created on: Aug 19, 2016
 *      Author: ubuntu
 */

#ifndef N900_N900PARSE_H_
#define N900_N900PARSE_H_

#include <stdio.h>
#include <iostream>
#include <syslog.h>
#include <vector>
#include "../common.h"

using namespace std;

#define N900_TAG_HEAD_LEN	4
#define N900_TAG_TAIL_LEN	4

#define N900_CARDATA	0
#define N900_GPSDATA	1
#define N900_REPORT		2
#define N900_BEATDATA	3
#define N900_FILETRANS	4

#define N900_RP_FAULT	0x01
#define N900_RP_STALLED	0x02 // 熄火
#define N900_RP_LOWVOL	0x03 
#define N900_RP_SHOCK	0x04 // 停车震动
#define N900_RP_ONPOW	0x05
#define N900_RP_VINCODE	0x06
#define N900_RP_IMSICODE	0x07
#define N900_RP_STATUS	0x08
#define N900_RP_EXCEPTN	0x09

#define N900_BT_TIME	0x00
#define N900_BT_CMD		0x01
#define N900_BT_RESPOND	0x02
#define N900_BT_AGPS	0x03

static unsigned char N900_TAG_HEAD[5] = { 0x55, 0x55, 0x55, 0x55 };
static unsigned char N900_TAG_TAIL[5] = { 0xAA, 0xAA, 0xAA, 0xAA };

struct subSecdata {
	int	id;		// 段号
	int	stime; 	// 时间(秒)
	float mileage; // 里程(公里)
	float fuelConsumption; // 油耗(升)
};

struct driveAct {
	int		id; // 编号
	int		count; // 次数
	vector<time_t> dtime; // 行为时间
};

struct faultCd {
	unsigned short id;		// 故障码
	unsigned short status; // 故障码状态
};

struct gpsInfo {
	float m_lat; // 纬度
	int m_latDir; // 纬度方向: 南北
	float m_lng; // 经度
	int m_lngDir; // 经度方向: 东西
	time_t m_time; // 时间
};

struct n900_msg {
	bool result;
	int errnum;
	time_t 	curtime;
	unsigned int obdid;
	unsigned short sequence;
	unsigned char type;
	time_t msg_time;

	int m_avgSpeed;
	int m_maxSpeed;
	int m_minSpeed;
	float m_fuleConsumptionTotal; // 累计油耗
	float m_mileageTotal; //累计里程
	int m_sectionDataCount; // 分段数据个数
	int m_driveActionCount; // 驾驶行为个数
	struct subSecdata *m_sec; // 分段数据
	struct driveAct *m_drive; // 驾驶行为

	/* 上报数据*/
	unsigned char m_type; // 上报数据类型
	unsigned m_faultCnt; // 故障码个数
	struct faultCd *m_fault; // 故障码
	time_t	m_offtime;

	float m_voltage; // 电瓶电压 

	unsigned char m_protype; // 当前车辆的OBD协议类型
	unsigned char m_marktype; // 识别车辆唯一标记类型
	char m_vinCode[20]; // VIN码
	char m_ismiCode[32]; // ISMI码

	unsigned char m_status; // 设备状态
	unsigned int m_exptype; // 异常类型

	/* gps 数据 */

	int m_gpscnt; // gps个数
	struct gpsInfo *m_gps; // GPS数据

	/* 心跳数据 */
//	unsigned char m_btype; // 心跳类型
	unsigned short m_version; // 软件版本

	unsigned int m_agpsLat; // agps请求的纬度
	unsigned int m_agpsLon; // agps 请求的经度
	unsigned int m_agpsId; //请求agps ID
	unsigned int m_index; // 未知数据

	unsigned char m_clearStatus; // 故障码清除状态 0 清除成功 1 失败
	unsigned char m_paramSetup; // 参数高管状态 0 成功 1: 失败

	unsigned char m_cmdType; // 命令响应类型

	bool checkvalid; 
	unsigned short m_engineSpeed; //发动机转速
	unsigned int m_loop_flag; // 开环或闭环
	unsigned short m_carspeed; // 车速
	unsigned short m_AirFuelRatio; 
	float m_airfuelRatio; // 目录空燃比
	unsigned short m_throttleOpen; // 节气门开度
	unsigned short m_engineTemp; // 发动机温度
	unsigned short m_inletTemp; // 进气温度
	unsigned short m_atomosPress; // 大气压力
	unsigned short m_inletPress; // 进气压力
	unsigned int m_carmileage; // 里程
	unsigned short m_AirFlow; 
	float m_airFlow; // 空气流量
	unsigned short m_CtlModeVol;
	float m_ctlModeVol; // 控制模块电压 
	unsigned short m_ignitionAngle; // 点火提前角

	unsigned int m_mile; // 车辆里程
	unsigned char m_logtype; // 日志类型
	size_t m_loglen; // 日志长度
	char *m_log; // 日志数据
	char m_file[32];
	unsigned short m_sequence;
};

int n900_parse_package(unsigned char *buf, size_t len, struct n900_msg *msg);
void n900_msg_free(struct n900_msg *msg);

#endif /* N900_N900PARSE_H_ */
